package com.sunyur.tailor.channel.web.outer.company.controller;

import com.alibaba.fastjson.JSON;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.enums.MiddleResultEnum;
import com.sunyur.tailor.channel.service.inner.company.param.PurchaserCompanyParam;
import com.sunyur.tailor.channel.service.inner.company.service.SunyurCompanyService;
import com.sunyur.tailor.channel.web.outer.company.dto.PurchaserCompanyDTO;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;

/**
 * @Author pengbing
 * @Description //公司主体控制层
 * @Date 15:45 2021/6/1
 * @Param
 * @return
 **/
@Log4j2
@RestController
@RequestMapping("/outer/company")
public class CompanyController {

    /**
     * 公司主体服务
     */
    @Autowired
    private SunyurCompanyService purchaserCompanyService;

    /**
     * 创建公司主体
     */
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public ChannelResult create(@RequestBody PurchaserCompanyDTO purchaserCompanyDTO) {
        log.info("CompanyController create param:{}", JSON.toJSONString(purchaserCompanyDTO));
        ChannelResult result = new ChannelResult<>();
        if (Objects.isNull(purchaserCompanyDTO)) {
            log.error("CompanyController create param is null");
            result.setError(MiddleResultEnum.PARAM_NULL.getCode(), MiddleResultEnum.PARAM_NULL.getValue());
            return result;
        }
        try {
            PurchaserCompanyParam purchaserCompanyParam = new PurchaserCompanyParam();
            BeanUtils.copyProperties(purchaserCompanyDTO,purchaserCompanyParam);
            result = purchaserCompanyService.createPurchaserCompany(purchaserCompanyParam);
        } catch (Exception e) {
            log.error("CompanyController create exception:{}", e);
            result.setError(MiddleResultEnum.EXCEPTION.getCode(), MiddleResultEnum.EXCEPTION.getValue());
        }
        return result;
    }
}
